/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.baseConfig.service.impl;

import com.koron.workflow.baseConfig.bean.DepartmentStaffBean;
import com.koron.workflow.baseConfig.bean.PostBean;
import com.koron.workflow.baseConfig.bean.PostStaffRelBean;
import com.koron.workflow.baseConfig.mapper.PostMapper;
import com.koron.workflow.baseConfig.mapper.StaffMapper;
import com.koron.workflow.baseConfig.service.PostService;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.DisposeBeanUtil;
import com.koron.workflow.common.util.WorkflowUtil;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author qingfeng
 * @description: TODO
 * @date 2020/4/13 11:52
 */
@Service
public class PostServiceImpl implements PostService {

    @Override
    @TaskAnnotation("queryForList")
    public PageBean<PostBean> queryForList(SessionFactory factory, String name, Integer page, Integer pageSize) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        List<PostBean> list = mapper.queryForList(name,page,pageSize);
        int total = mapper.countForList(name);
        PageBean<PostBean> result = new PageBean<>(list,total);
        return result;
    }

    @Override
    @TaskAnnotation("save")
    public MessageBean<String> save(SessionFactory factory, PostBean post, StaffBean user) {
        MessageBean<String> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(),"保存成功",String.class);
        PostMapper mapper = factory.getMapper(PostMapper.class);
        PostBean bean = mapper.queryByCode(post.getCode());
        if (StringUtils.isEmpty(post.getId())) {
            DisposeBeanUtil.disposeInsert(user,post);
            if (bean != null) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("编码已存在");
                return msg;
            }
            mapper.insert(post);
        } else {
            DisposeBeanUtil.disposeUpdate(user,post);
            if (bean != null && !bean.getId().equals(post.getId())) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("编码已存在");
                return msg;
            }
            mapper.update(post);
        }
        return msg;
    }

    @Override
    @TaskAnnotation("delete")
    public void delete(SessionFactory factory, String id) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        mapper.delete(id);
    }

    @Override
    @TaskAnnotation("queryForObject")
    public PostBean queryForObject(SessionFactory factory, String id) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        return mapper.queryForObject(id);
    }

    @Override
    @TaskAnnotation("deletePostStaffRel")
    public void deletePostStaffRel(SessionFactory factory, String id) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        mapper.deletePostStaffRel(id,null);
    }

    @Override
    @TaskAnnotation("queryPostStaff")
    public List<DepartmentStaffBean> queryPostStaff(SessionFactory factory, String postId, String departmentName, String staffName) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        return mapper.queryPostStaff(postId, departmentName, staffName);
    }

    @Override
    @TaskAnnotation("updatePostStaffRel")
    public void updatePostStaffRel(SessionFactory factory, String postId, String staffId) {
        PostMapper mapper = factory.getMapper(PostMapper.class);
        mapper.deletePostStaffRel(null,postId);
        if (!StringUtils.isEmpty(staffId)) {
            String[] ids = staffId.split(",");
            List<PostStaffRelBean> list = new ArrayList<>();
            for (String id : ids) {
                PostStaffRelBean bean = new PostStaffRelBean();
                bean.setId(WorkflowUtil.getObjectId());
                bean.setPostId(postId);
                bean.setStaffId(id);
                list.add(bean);
            }
            mapper.updatePostStaffRel(list);
        }
    }

	@Override
	public List<StaffBean> queryPostStaff(SessionFactory factory, String postId) {
		 PostMapper mapper = factory.getMapper(PostMapper.class);
		 StaffMapper staffMapper = factory.getMapper(StaffMapper.class);
		 List<DepartmentStaffBean> departments = mapper.queryPostStaff(postId, null, null);
		 return departments.stream().map(t -> staffMapper.queryByAccount(t.getAccount())).collect(Collectors.toList());
	}
}
